package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.model.carrier.Carrier;
import com.deer.wms.system.manage.model.carrier.CarrierCriteria;
import com.deer.wms.system.manage.model.carrier.CarrierDto;
import com.deer.wms.system.manage.service.CarrierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/13.
*/
@Api(description = "快递信息api接口")
@RestController
@RequestMapping("/carriers")
public class CarrierController {

    @Autowired
    private CarrierService carrierService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/insert")
    public Result add(@RequestBody Carrier carrier) {
        carrierService.save(carrier);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer carrierId) {
        carrierService.removeById(carrierId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Carrier carrier) {
        carrierService.updateById(carrier);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@PathVariable Integer id) {
        Carrier carrier = carrierService.getById(id);
        return ResultGenerator.genSuccessResult(carrier);
    }


    @ApiOperation(value = "快递信息", notes = "快递信息")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @GetMapping("/list")
    public Result list(CarrierCriteria criteria , @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<CarrierDto> list = carrierService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/allList")
    public Result allList(@ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

//        List<Carrier> list = (List<Carrier>) redisUtil.get("allCarrier");
//        if(list==null || list.size()==0){
//            list = carrierService.list();
//            redisUtil.set("allCarrier",list);
//        }
        List<Carrier>    list = carrierService.list();

        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
